import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Role } from './entities/role.entity';
import { Repository } from 'typeorm';

@Injectable()
export class RoleService {
  constructor(@InjectRepository(Role) private readonly roleRepo: Repository<Role>) {}

  async getRoles() {
    return await this.roleRepo.find();
  }

  async findPermissionIdByRoleId(roleId: number) {
    const roleInfo = await this.roleRepo.findOne({
      where: { id: roleId },
      select: ['permissions'],
      relations: {
        permissions: true,
      },
    });
    const permissionIds = roleInfo.permissions.map((item) => item.id);

    return permissionIds;
  }
}
